package com.pcrc.app;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Button;
import android.widget.EditText;


public class write extends Activity {
    /** Called when the activity is first created. */
    EditText edittitle;
    EditText editstc;   
    
    Intent intent = getIntent();
    public String sid;
    public String id;
    HttpResponse response;
    HttpClient httpclient;
    String endResult;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write);
        sid=getIntent().getStringExtra("sid");
        id=getIntent().getStringExtra("id");
        //id=getIntent().getStringExtra("id");
        CookieManager cookie=CookieManager.getInstance();
        cookie.setCookie("http://pcrc.co.kr","PHPSESSID="+sid);
        CookieSyncManager.getInstance().sync();
        
        edittitle = (EditText)findViewById(R.id.editsubject);
        editstc = (EditText)findViewById(R.id.editmemo);
        
        try {
            
            HttpPost post=new HttpPost("http://pcrc.co.kr/board/write_ok.php");
            post.setHeader("Cookies", sid);
            
            httpclient.execute(post);
            response = null;
		}
		catch(Exception ex)
		{}
        
        
        Button check = (Button)findViewById(R.id.check);
        check.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {

        		try {
                    HttpParams params = new BasicHttpParams();
                    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                    HttpConnectionParams.setConnectionTimeout(params, 5000);
                    HttpConnectionParams.setSoTimeout(params, 5000);
         
                    HttpClient client = new DefaultHttpClient(params);
                    String strUrl = "http://pcrc.co.kr/board/write_ok.php";
                    HttpPost post = new HttpPost(strUrl);

         // Header 세팅이 필요한 경우
                    post.addHeader("Cookie", "PHPSESSID="+sid);
                    post.addHeader("Host", "pcrc.co.kr");
                    post.addHeader("Referer", "http://pcrc.co.kr/board/write.php?id="+id+"&mode=write");
                    Log.e("sid", sid);
         
                    MultipartEntity entry = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
         
                    // post data 세팅.. UTF-8 로 인코딩처리
                    Map m_hashmap = new HashMap();
                    EditText editsubject=(EditText)findViewById(R.id.editsubject);
                    EditText editmemo=(EditText)findViewById(R.id.editmemo);
                    m_hashmap.put("page","1");
                    m_hashmap.put("id",id);
                    m_hashmap.put("no","");
                    m_hashmap.put("select_arrange","headnum");
                    m_hashmap.put("desc","asc");
                    m_hashmap.put("page_num","");
                    m_hashmap.put("keyword","");
                    m_hashmap.put("category","");
                    m_hashmap.put("sn","off");
                    m_hashmap.put("ss","on");
                    m_hashmap.put("sc","on");
                    m_hashmap.put("mode","write");
                    m_hashmap.put("subject",editsubject.getText().toString());
                    m_hashmap.put("memo", editmemo.getText().toString());
                    Iterator<String> keys = m_hashmap.keySet().iterator();
                    while (keys.hasNext()) {
                        String key = (String) keys.next();
                        entry.addPart(key, new StringBody((String)m_hashmap.get(key), "text/plain", Charset.forName("euc-kr")));
                    }

                    // 파일 객체를 넘겨받던지.. 단말 리소스에서 추출하던지.. 어쨓든 File 객체를 얻어낸다.
                    //File file = new File("C:\\temp\\nagasu.jpg");
                    //ContentBody fileBody = new FileBody(file, "image/jpeg");
                    //entry.addPart("id", fileBody);
         
                    post.setEntity(entry);
                    
                    HttpResponse response = client.execute(post);
                    int status = response.getStatusLine().getStatusCode();
         
                    System.out.println("status:"+status);
                    if (status == HttpStatus.SC_OK) {
                       // 성공
                    }
                    
                    if(id.equals("yb"))
                    {
                    	Intent intent=new Intent(getApplicationContext(),yb.class);
                		intent.putExtra("sid", sid);
                		startActivityForResult(intent,0);
                    }
                    else if(id.equals("ob"))
                    {
                    	Intent intent=new Intent(getApplicationContext(),ob.class);
                		intent.putExtra("sid", sid);
                		startActivityForResult(intent,0);
                    }
                    else if(id.equals("notice"))
                    {
                    	Intent intent=new Intent(getApplicationContext(),pcrcnotice.class);
                		intent.putExtra("sid", sid);
                		startActivityForResult(intent,0);
                    }
                    else if(id.equals("study"))
                    {
                    	Intent intent=new Intent(getApplicationContext(),study.class);
                		intent.putExtra("sid", sid);
                		startActivityForResult(intent,0);
                    }
                    else if(id.equals("gallery"))
                    {
                    	Intent intent=new Intent(getApplicationContext(),gallery.class);
                		intent.putExtra("sid", sid);
                		startActivityForResult(intent,0);
                    }
                  
                } catch (Exception e) {
                    e.printStackTrace();
                }
        	}
        }
        );
        // 취소 버튼을 누르면 이전 뷰로 전환하는 부분
        Button cancle = (Button)findViewById(R.id.cancle);
        cancle.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		//setResult(RESULT_CANCELED);
        		//finish();
        		if(id.equals("yb"))
                {
                	Intent intent=new Intent(getApplicationContext(),yb.class);
            		intent.putExtra("sid", sid);
            		startActivityForResult(intent,0);
                }
                else if(id.equals("ob"))
                {
                	Intent intent=new Intent(getApplicationContext(),ob.class);
            		intent.putExtra("sid", sid);
            		startActivityForResult(intent,0);
                }
                else if(id.equals("notice"))
                {
                	Intent intent=new Intent(getApplicationContext(),pcrcnotice.class);
            		intent.putExtra("sid", sid);
            		startActivityForResult(intent,0);
                }
                else if(id.equals("study"))
                {
                	Intent intent=new Intent(getApplicationContext(),study.class);
            		intent.putExtra("sid", sid);
            		startActivityForResult(intent,0);
                }
                else if(id.equals("gallery"))
                {
                	Intent intent=new Intent(getApplicationContext(),gallery.class);
            		intent.putExtra("sid", sid);
            		startActivityForResult(intent,0);
                }
        	}
        });
	}
    
}